package com.tjcd.project.library_base.common.widget;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.coorchice.library.SuperTextView;

import java.util.List;

/**
 * @Author jiangKunKun
 * @CreateDate 2021/5/21
 * @Description 跑马灯上下
 * @SpittingGrooves
 */
public class ScrollTextView extends LinearLayout {
    private RelativeLayout mLlBanner1, mLlBanner2;
    private SuperTextView mTypeStv1, mTypeStv2;
    private TextView mContext1, mContext2, mTime1, mTime2;
    private Handler handler;
    private boolean isShow = false;
    private int startY1, endY1, startY2, endY2;
    private Runnable runnable;
    private int position = 0;
    private int offsetY = 100;
    private boolean hasPostRunnable = false;
    private OnContextScrollListener mContextScrollListener;
    private List<Object> mList;


    public OnContextScrollListener getContextScrollListener(){
        return mContextScrollListener;
    }

    public void setContextScrollListener(OnContextScrollListener contextScrollListener){
        mContextScrollListener = contextScrollListener;
    }

    public interface OnContextScrollListener {
        void onScrollStatePosition(int position);
    }

    public ScrollTextView(Context context) {
        this(context, null);
    }

    public ScrollTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ScrollTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

//        View view = LayoutInflater.from(context).inflate(R.layout.widget_scroll_text_layout, this);
//        mLlBanner1 = view.findViewById(R.id.ll_banner1);
//        mLlBanner2 = view.findViewById(R.id.ll_banner2);
//        mTypeStv1 = view.findViewById(R.id.home_msg_type_stv1);
//        mTypeStv2 = view.findViewById(R.id.home_msg_type_stv2);
//        mContext1 = view.findViewById(R.id.home_msg_context1);
//        mContext2 = view.findViewById(R.id.home_msg_context2);
//        mTime1 = view.findViewById(R.id.home_msg_time1);
//        mTime2 = view.findViewById(R.id.home_msg_time2);
//        handler = new Handler();
//        runnable = new Runnable() {
//            @Override
//            public void run() {
//                isShow = !isShow;
//                if (position == mList.size() - 1) {
//                    position = 0;
//                }
//
//                if (isShow) {
//                    // 视图一
//                    Assignment1(mList.get(position++));
//
//                    // 视图二
//                    Assignment2(mList.get(position));
//                } else {
//                    // 视图一
//                    Assignment2(mList.get(position++));
//
//                    // 视图二
//                    Assignment1(mList.get(position));
//                }
//
//                startY1 = isShow ? 0 : offsetY;
//                endY1 = isShow ? -offsetY : 0;
//                ObjectAnimator.ofFloat(mLlBanner1, "translationY", startY1, endY1).setDuration(300).start();
//
//                startY2 = isShow ? offsetY : 0;
//                endY2 = isShow ? 0 : -offsetY;
//                ObjectAnimator.ofFloat(mLlBanner2, "translationY", startY2, endY2).setDuration(300).start();
//
//                handler.postDelayed(runnable, 3000);
//                if (null != mContextScrollListener){
//                    mContextScrollListener.onScrollStatePosition(position);
//                }
//            }
//        };
    }
//
//    private void Assignment2(NoticeTotalBean.DataBean.ListBean listBean){
//        if ("manual".equals(listBean.getType())){
//            mTypeStv2.setText("公告");
//            mTypeStv2.setSolid(getResources().getColor(R.color.red_FF642B));
//        }else {
//            mTypeStv2.setText("消息");
//            mTypeStv2.setSolid(getResources().getColor(R.color.orange_FF9A0D));
//        }
//        mContext2.setText(listBean.getTitle());
//        mTime2.setText(listBean.getDateDiff());
//    }
//
//    public void Assignment1(NoticeTotalBean.DataBean.ListBean listBean){
//        if ("manual".equals(listBean.getType())){
//            mTypeStv1.setText("公告");
//            mTypeStv1.setSolid(getResources().getColor(R.color.red_FF642B));
//        }else {
//            mTypeStv1.setText("消息");
//            mTypeStv1.setSolid(getResources().getColor(R.color.orange_FF9A0D));
//        }
//        mContext1.setText(listBean.getTitle());
//        mTime1.setText(listBean.getDateDiff());
//    }
//
//    public List<NoticeTotalBean.DataBean.ListBean> getList() {
//        return mList;
//    }
//
//    public void setList(List<NoticeTotalBean.DataBean.ListBean> list) {
//        this.mList = list;
//
//        //处理最后一条数据切换到第一条数据 太快的问题
//        if (list.size() > 1) {
//            list.add(list.get(0));
//        }
//    }
//
//    public void startScroll() {
//        Assignment1(mList.get(0));
//        if (mList.size() > 1) {
//            if(!hasPostRunnable) {
//                hasPostRunnable = true;
//                //处理第一次进入 第一条数据切换第二条 太快的问题
//                handler.postDelayed(runnable,3000);
//            }
//        } else {
//            //只有一条数据不进行滚动
//            hasPostRunnable = false;
////            mBannerTV1.setText(list.get(0));
//        }
//    }

    public void stopScroll() {
        if (null != runnable){
            handler.removeCallbacks(runnable);
        }
        hasPostRunnable = false;
    }

}
